// 云函数入口文件
const cloud = require('wx-server-sdk')
const axios = require('axios')
axios.defaults.baseURL = 'http://139.199.203.14:4000';
cloud.init()
const db = cloud.database();
const collectionPlaylist = db.collection('playlist');
// 云函数入口函数
exports.main = async (event, context) => {
  let {data:res} = await axios.get(`/personalized?limit=30`)
  let lists = res.result;
  let {data:dbLists} = await collectionPlaylist.get()
  console.log('response:',lists,dbLists)
 
  for(let i = 0; i< lists.length;i++) {
    let flag = true
    let item = lists[i]
    for (let j = 0; j< dbLists.length; j++) {
      let sub = dbLists[j]
      if (sub.id === item.id) {
        flag = false
        break;
      }
    }
    if (flag) {
      item.createTime = item.trackNumberUpdateTime
      delete item.trackNumberUpdateTime
      await collectionPlaylist.add({
        data:item
      })
      flag = true
    }
  } 
  return 0
}